package cn.xopencode.recordservice.dal.mysql.mapper.meterroom;

import cn.xopencode.common.framework.util.DateUtil;
import cn.xopencode.mybatis.core.query.QueryWrapperX;
import cn.xopencode.recordservice.dal.mysql.dataobject.meterroom.MeterRoomRecordDO;
import cn.xopencode.recordservice.service.meterroom.bo.MeterRoomRecordPageBO;
import cn.xopencode.recordservice.service.robotroom.bo.AdminRecordPageBO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface MeterRoomRecordMapper extends BaseMapper<MeterRoomRecordDO> {
    default IPage<MeterRoomRecordDO> selectPage(MeterRoomRecordPageBO pageBO){
        if (pageBO.getInspectionTime() != null){
            String inspectionTime = DateUtil.format(pageBO.getInspectionTime(),"yyyy-MM-dd");
            return selectPage(new Page<>(pageBO.getPageNo(),pageBO.getPageSize()),
                    new QueryWrapperX<MeterRoomRecordDO>()
            .betweenIfPresent("inspection_time",inspectionTime + " 00:00:00 ",inspectionTime + " 23:59:59 ")
            .orderByDesc(" is_notice_leader = 1 and is_leader_deal = 0 ")
            .orderByDesc("inspection_time"));
        }
        return selectPage(new Page<>(pageBO.getPageNo(),pageBO.getPageSize()),
                new QueryWrapperX<MeterRoomRecordDO>().orderByDesc(" is_notice_leader = 1 and is_leader_deal = 0 ")
                    .orderByDesc("inspection_time"));
    }

    /**
     * 获取最新的一条记录
     */
    default MeterRoomRecordDO getLastRecord(){
        return selectOne(new QueryWrapperX<MeterRoomRecordDO>()
            .orderByDesc("inspection_time").last("limit 1"));
    }

    /**
     * 后台按条件分页查询列表数据
     * @param pageBO
     * @return
     */
    IPage<MeterRoomRecordDO> adminQuery(Page page, @Param("pageBO") AdminRecordPageBO pageBO);
}
